package com.ruoyi.mall.mq.config;

import com.ruoyi.mall.constant.OrderPaymentQueueExchangeContant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {


    @Value("-${spring.profiles.active}")
    private String profile;

  /*  //配置一个Direct类型的交换机
    @Bean
    public DirectExchange orderDirectExchange() {
        return new DirectExchange(OrderPaymentQueueExchangeContant.ORDER_PAYMENT_EXCHANGE);
    }

    //配置一个队列
    @Bean
    public Queue orderDirectQueue() {
//        return new Queue(OrderPaymentQueueExchangeContant.ORDER_PAYMENT_QUEUE+profile);
        return QueueBuilder.durable(OrderPaymentQueueExchangeContant.ORDER_PAYMENT_QUEUE + profile).build();

    }

    *//**
     * 配置一个队列和交换机的绑定
     * @param directQueue : 需要绑定的队列对象，参数名必须和某个@Bean的方法名完全相同，这样就会进行自动注入，对应 .bind()
     * @param directExchange : 需要绑定的交换机对象，参数名必须和某个@Bean的方法名完全相同，这样就会进行自动注入，对应 .to()
     *                       .with() 方法对应的RoutingKey
     * @return
     *//*
    @Bean
    public Binding orderDirectBinding(@Qualifier("orderDirectQueue") Queue directQueue,@Qualifier("orderDirectExchange")  DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with(OrderPaymentQueueExchangeContant.ORDER_PAYMENT_ROUTING);
    }*/

    @Bean
    Queue queue() {
        return new Queue(OrderPaymentQueueExchangeContant.ORDER_PAYMENT_QUEUE + profile, true, false, false);
    }

    @Bean
    CustomExchange customExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(OrderPaymentQueueExchangeContant.ORDER_PAYMENT_EXCHANGE, "x-delayed-message", true, false,args);
    }

    @Bean
    Binding binding() {
        return BindingBuilder.bind(queue())
                .to(customExchange()).with(OrderPaymentQueueExchangeContant.ORDER_PAYMENT_QUEUE + profile).noargs();
    }

}
